<?php

namespace App\Http\Controllers\Admin;

use App\Models\Brand;
use App\Models\Goods;
use App\Models\GoodsCategory;
use App\Models\Picture;
use App\Models\Shop;
use App\Models\ShopBucketCategory;
use App\Models\ShopGoods;

/**
 * 商品管理控制器
 */
class ShopGoodsController extends BaseController
{

    public function __construct(
        Goods $goods,
        Shop $shop,
        GoodsCategory $goodsCategory,
        Brand $brand,
        Picture $picture,
        ShopBucketCategory $shopBucketCategory,
        ShopGoods $shopGoods
    )
    {
        parent::__construct();
        $this->goods = $goods;
        $this->shopGoods = $shopGoods;
        $this->shop = $shop;
        $this->goodsCategory = $goodsCategory;
        $this->shopBucketCategory = $shopBucketCategory;
        $this->brand = $brand;
        $this->picture = $picture;
    }


    public function getIndex()
    {
        $rows = $this->shopGoods->getOwnGoodsListById(request('shop_id'));
        $ext = $this->picture->config['saveExt'];
        $mini = 60;
        if ($this->picture->config['cut_Mini_Size']) {
            $miniArr = explode(',', $this->picture->config['cut_Mini_Size']);
            $mini = $miniArr[0];
        }
        return view('admin.shop_goods.index', compact('rows', 'ext', 'mini'));
    }


    public function getInfo()
    {
        $info = $this->shopGoods->_getOneById(request('id'));
        return view('admin.shop_goods.info', compact('info'));
    }

    public function getAdd()
    {
        $goodsCategory = $this->goodsCategory->getCategoryTree();
        unset($goodsCategory[0]);
        $brand = $this->brand->_getList([], '*', ['sort' => 'asc'])->toArray();
        $bucketCategory = $this->shopBucketCategory->_getOne(['shop_id'=>request('shop_id')],'category_data');
        if (!$bucketCategory) {
            return $this->error('请先添加水桶品牌');
        }
        $bucketCategory = $bucketCategory['category_data'];
        $ext = $this->picture->config['saveExt'];
        return view('admin.shop_goods.edit', compact('goodsCategory', 'bucketCategory', 'brand', 'ext'));
    }

    public function postAdd()
    {
        $res = $this->goods->addOrUpdate();
        if (!$res) {
            return $this->error($this->goods->getError() ? $this->goods->getError() : '未知错误');
        }
        return $this->success('新增成功', urlAdmin('shopGoods', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    /* 编辑分类 */
    public function getEdit()
    {
        $goodsCategory = $this->goodsCategory->getCategoryTree();
        unset($goodsCategory[0]);
        $brand = $this->brand->_getList([], '*', ['sort' => 'asc'])->toArray();
        $info = $this->shopGoods->_getOneById(request('id'));
        $info = $info ? $info->toArray() : [];
        $bucketCategory = $this->shopBucketCategory->_getOne(['shop_id'=>request('shop_id')],'category_data');
        if (!$bucketCategory) {
            return $this->error('请先添加水桶品牌');
        }
        $bucketCategory = $bucketCategory['category_data'];
        $ext = $this->picture->config['saveExt'];
        return view('admin.shop_goods.edit', compact('goodsCategory', 'bucketCategory', 'brand', 'info', 'ext'));
    }

    public function postEdit()
    {
        $res = $this->shopGoods->addOrUpdate();
        if (!$res) {
            return $this->error($this->shopGoods->getError() ? $this->shopGoods->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('shopGoods', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    public function getStore()
    {
        if (!request('shop_id')) {
            return $this->error('非法操作');
        }
        $ids = [];
        $goodsList = $this->shopGoods->_getList(['shop_id' => request('shop_id')],'goods_id');
        if ($goodsList) {
            $goodsList = $goodsList->toArray();
            foreach ($goodsList as $k => $v) {
                $ids[] = $v['goods_id'];
            }
        }
        $rows = $this->goods->_getList(['admin_id' => is_login(), 'status' => 1], '*', ['sort' => 'asc']);
        if ($rows) {
            $rows = $rows->toArray();
        }
        return view('admin.shop_goods.store', compact('rows', 'goodsList','ids'));
    }

    public function postStore()
    {
        $res = $this->shopGoods->addFromStore();
        if (!$res) {
            return $this->error($this->shopGoods->getError() ? $this->shopGoods->getError() : '未知错误');
        }
        return $this->success('操作成功', urlAdmin('shopGoods', 'getIndex', ['shop_id' => request('shop_id')]));
    }


    //上传
    public function postUpload()
    {
        $res = $this->goods->uploadPicture();
        return $this->ajaxReturn($res);
    }

    public function anyChangeStatus()
    {
        $method = request('method');
        $ids = array_unique((array)request('ids'));
        if (empty($ids)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $shopId = request('shop_id');
        $where['id'] = ['in', $ids];
        switch (strtolower($method)) {
            case 'up':
                $res = $this->shopGoods->_update($where,['status'=>1]);
                if(!$res) {
                    return $this->error('上架失败!');
                }
                $this->shopGoods->_redisGenerateGoodsListBySid($shopId);
                return $this->success('上架成功!');
            case 'down':
                $res = $this->shopGoods->_update($where,['status'=>-1]);
                if(!$res) {
                    return $this->error('下架失败!');
                }
                $this->shopGoods->_redisGenerateGoodsListBySid($shopId);
                return $this->success('下架成功!');
            default:
                return $this->error('参数非法');
        }
    }

    /**
     * 排序
     */
    public function getSort()
    {
        $where['status'] = 1;
        $where['admin_id'] = is_login();
        if (request()->has('ids')) {
            $where['id'] = ['in', [request('ids')]];
        }
        if (request('shop_id')) {
            $where['shop_id'] = intval(request('shop_id'));
        }
        $rows = $this->shopGoods->_getList($where, 'id,goods_name', ['sort' => 'asc'])->toArray();
        return view('admin.shop_goods.sort', compact('rows'));
    }

    public function postSort()
    {
        $ids = request('ids');
        $ids = explode(',', $ids);
        $data = [];
        foreach ($ids as $key => $value) {
            $data[] = ['id' => $value, 'sort' => $key + 1];
        }
        $res = $this->shopGoods->_updateBatch($data);
        if (!$res) {
            return $this->error('排序失败！');
        }
        $this->shopGoods->_redisGenerateGoodsListBySid(request('shop_id'));
        return $this->success('排序成功！');
    }

}
